package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppClubActivityService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppClubActivityCreateValidate;
import com.mdd.admin.validate.yylq.TAppClubActivitySearchValidate;
import com.mdd.admin.validate.yylq.TAppClubActivityUpdateValidate;
import com.mdd.admin.vo.yylq.TAppClubActivityDetailVo;
import com.mdd.admin.vo.yylq.TAppClubActivityListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppClubActivity;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppClubActivityMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 俱乐部活动实现类
 * @author LikeAdmin
 */
@Service
public class TAppClubActivityServiceImpl implements ITAppClubActivityService {
        
    @Resource
    TAppClubActivityMapper tAppClubActivityMapper;

    /**
     * 俱乐部活动列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppClubActivityListedVo>
     */
    @Override
    public PageResult<TAppClubActivityListedVo> list(PageValidate pageValidate, TAppClubActivitySearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppClubActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppClubActivityMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:activityName@activity_name:str",
            "=:pic:str",
            "=:introduction:str",
            "=:activityStartTime@activity_start_time:str",
            "=:activityEndTime@activity_end_time:str",
            "=:signupStartTime@signup_start_time:str",
            "=:signupEndTime@signup_end_time:str",
            "=:dicId@dic_id:int",
            "=:dicType@dic_type:str",
            "=:status:int",
            "=:provinceCode@province_code:str",
            "like:provinceName@province_name:str",
            "=:cityCode@city_code:str",
            "like:cityName@city_name:str",
            "=:areaCode@area_code:str",
            "like:areaName@area_name:str",
            "=:address:str",
            "=:shouAddress@shou_address:str",
            "=:activityMaxAmount@activity_max_amount:int",
        });

        IPage<TAppClubActivity> iPage = tAppClubActivityMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppClubActivityListedVo> list = new LinkedList<>();
        for(TAppClubActivity item : iPage.getRecords()) {
            TAppClubActivityListedVo vo = new TAppClubActivityListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 俱乐部活动详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppClubActivity
     */
    @Override
    public TAppClubActivityDetailVo detail(Integer id) {
        TAppClubActivity model = tAppClubActivityMapper.selectOne(
                new QueryWrapper<TAppClubActivity>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppClubActivityDetailVo vo = new TAppClubActivityDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 俱乐部活动新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppClubActivityCreateValidate createValidate) {
        TAppClubActivity model = new TAppClubActivity();
        model.setActivityName(createValidate.getActivityName());
        model.setPic(createValidate.getPic());
        model.setIntroduction(createValidate.getIntroduction());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setActivityStartTime(createValidate.getActivityStartTime());
        model.setActivityEndTime(createValidate.getActivityEndTime());
        model.setSignupStartTime(createValidate.getSignupStartTime());
        model.setSignupEndTime(createValidate.getSignupEndTime());
        model.setDicId(createValidate.getDicId());
        model.setDicType(createValidate.getDicType());
        model.setStatus(createValidate.getStatus());
        model.setProvinceCode(createValidate.getProvinceCode());
        model.setProvinceName(createValidate.getProvinceName());
        model.setCityCode(createValidate.getCityCode());
        model.setCityName(createValidate.getCityName());
        model.setAreaCode(createValidate.getAreaCode());
        model.setAreaName(createValidate.getAreaName());
        model.setAddress(createValidate.getAddress());
        model.setShouAddress(createValidate.getShouAddress());
        model.setActivityMaxAmount(createValidate.getActivityMaxAmount());
        tAppClubActivityMapper.insert(model);
    }

    /**
     * 俱乐部活动编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppClubActivityUpdateValidate updateValidate) {
        TAppClubActivity model = tAppClubActivityMapper.selectOne(
                new QueryWrapper<TAppClubActivity>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setActivityName(updateValidate.getActivityName());
        model.setPic(updateValidate.getPic());
        model.setIntroduction(updateValidate.getIntroduction());
        model.setUpdateTime(new Date());
        model.setActivityStartTime(updateValidate.getActivityStartTime());
        model.setActivityEndTime(updateValidate.getActivityEndTime());
        model.setSignupStartTime(updateValidate.getSignupStartTime());
        model.setSignupEndTime(updateValidate.getSignupEndTime());
        model.setDicId(updateValidate.getDicId());
        model.setDicType(updateValidate.getDicType());
        model.setStatus(updateValidate.getStatus());
        model.setProvinceCode(updateValidate.getProvinceCode());
        model.setProvinceName(updateValidate.getProvinceName());
        model.setCityCode(updateValidate.getCityCode());
        model.setCityName(updateValidate.getCityName());
        model.setAreaCode(updateValidate.getAreaCode());
        model.setAreaName(updateValidate.getAreaName());
        model.setAddress(updateValidate.getAddress());
        model.setShouAddress(updateValidate.getShouAddress());
        model.setActivityMaxAmount(updateValidate.getActivityMaxAmount());
        tAppClubActivityMapper.updateById(model);
    }

    /**
     * 俱乐部活动删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppClubActivity model = tAppClubActivityMapper.selectOne(
                new QueryWrapper<TAppClubActivity>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppClubActivityMapper.delete(new QueryWrapper<TAppClubActivity>().eq("id", id));
    }

}
